package mmc.cell;

import java.io.Serializable;
import static mmc.util.MyMath.*;

import mmc.Constants;
import mmc.EngineObject;

@SuppressWarnings("serial")
public class CellPosition implements Comparable<CellPosition>, Serializable{
	public int x, y, z;
	public static int SEED=0;
	
	public CellPosition(EngineObject o){
		x = floor(o.x)&Constants.CELL_MASK;
		y = floor(o.y)&Constants.CELL_MASK;
		z = floor(o.z)&Constants.CELL_MASK;
	}
	
	public CellPosition(EngineObject o, int mask){
		x = floor(o.x)&mask;
		y = floor(o.y)&mask;
		z = floor(o.z)&mask;
	}
	
	public CellPosition(int x, int y, int z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}
	
	/**
	 * Inverse of toString()
	 * 
	 * @param s
	 */
	public CellPosition(String s){
		String[] ss = s.substring(1, s.length()-1).split(",");
		x = new Integer(ss[0]);
		y = new Integer(ss[1]);
		z = new Integer(ss[2]);
	}

	@Override
	public String toString() {
		return "("+x+","+y+","+z+")";
	}

	@Override
	public int compareTo(CellPosition o) {
		if(x==o.x){
			if(y==o.y){
				return z-o.z;
			}else
				return y-o.y;
		}else
			return x-o.x;
	}
	
	public boolean equals(CellPosition o){
		return x==o.x && y==o.y && z==o.z;
	}
	
	public long calculateSeed(){
		return (x>>3)^(y>>1)^z^SEED;
	}
}